package com.efounder.activity;

import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.efounder.adapter.SelectAddressAdapter;
import com.efounder.bean.Address;
import com.efounder.chat.activity.BaseActivity;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.http.EFHttpRequest;
import com.efounder.mobilemall.R;
import com.efounder.utils.MallRequestUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.efounder.utils.MallRequestUtil.ADDRESS_LIST_URL;

/**
 * 地址选择列表
 * Created by will on 18-3-15.
 */

public class MallSelectAddressActivity extends BaseActivity implements View.OnClickListener {

    private String TAG = this.getClass().getSimpleName();
    private String addressId;
    private List<Address> addresses = new ArrayList<>();
    private SelectAddressAdapter addressListAdapter;
    public static final int ADD_ADDRESS_REQUEST_CODE = 8803;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mall_layout_address_list);
        if (getIntent().getExtras() != null) {
            addressId = getIntent().getExtras().getString("addressId");
        }
        ImageView ivBack = (ImageView) findViewById(R.id.iv_back);
        ivBack.setOnClickListener(this);
        TextView tvTitle = (TextView) findViewById(R.id.tv_title);
        tvTitle.setText(R.string.mail_choose_receive_address);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initData();
        if (addressListAdapter != null) {
            addressListAdapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        for (Address address : addresses) {
            if (address.getAddressId().equals(addressId)) {
                address.setCurrentSelected(true);
            }
        }
        addressListAdapter = new SelectAddressAdapter(addresses, this);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(addressListAdapter);
        addressListAdapter.setOnAddressSelectedListener(new SelectAddressAdapter.OnAddressSelectedListener() {
            @Override
            public void onAddressSelected(int position) {
                Intent intent = new Intent();
                intent.putExtra("addressId", addresses.get(position).getAddressId());
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        addressListAdapter.setOnShowEditListener(new SelectAddressAdapter.OnShowEditAddressListener() {
            @Override
            public void onShowEditPop(int position) {
                Intent intent = new Intent(MallSelectAddressActivity.this, MallEditAddressNewActivity.class);
                intent.putExtra("address", (Serializable) addresses);
                intent.putExtra("position", position);
                startActivity(intent);
            }
        });
        TextView tvAddNew = (TextView) findViewById(R.id.tv_add_new_address);
        tvAddNew.setOnClickListener(this);
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("userId", EnvironmentVariable.getUserName());
        MallRequestUtil.getRequest(TAG, ADDRESS_LIST_URL, map, new MallRequestUtil.MallRequestCallback() {
            @Override
            public void onSuccess(String response) {
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    JSONArray jsonArray = jsonObject.getJSONArray("addresses");
                    if (jsonArray.length() > 0) {
                        addresses.clear();
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject addressObject = jsonArray.getJSONObject(i);
                            Address address = new Address();
                            address.setAddressId(addressObject.getString("addressId"));
                            address.setAddress(addressObject.getString("address"));
                            address.setArea(addressObject.getString("area"));
                            address.setIsSelected(addressObject.getInt("isSelected") + "");
                            address.setUserName(addressObject.getString("userName"));
                            address.setPhone(addressObject.getString("phone"));
//                            if (address.getIsSelected().equals("1")) {
//                                defaultAddress = address;
//                            }
                            addresses.add(address);
                        }
                        initView();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFail(String error) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.iv_back) {
            Intent intent = new Intent();
            intent.putExtra("addressId", addressId);
            setResult(RESULT_OK, intent);

            finish();
        } else if (id == R.id.tv_add_new_address) {
            //增加新地址
            Intent intent = new Intent(this, MallAddAddressNewActivity.class);
            startActivity(intent);
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        EFHttpRequest.cancelRequest(TAG);
    }
}